﻿var cti = new CTI("http://101.205.3.64:8081/cti/");
//显示或隐藏图片按钮和点击事件
//id为a标签的id
//picsrc为图片路径
//enable为是否添加点击事件0为取消，其他为添加
//btnsstat为按钮的两种状态比如签入和签出，一般来说与enable的参数保持一致就行
function showImage(id,picsrc,enable,btnsstat) {
	var elementimg;//图片
	var elementa;//链接
	elementimg = document.getElementById(id);
	elementimg.src = picsrc;

	if (enable==0){
		//elementimg.className = "grayscale";//图片变灰
		elementa= document.getElementById(id + "a");
		elementa.style.cursor='default'; //改变鼠标形状
		elementa.onclick=''; //取消onclick事件
	}
	else{
		//elementimg.className = "";
		elementa= document.getElementById(id + "a");
		elementa.style.cursor='auto'; //改变鼠标形状
		elementa.onclick=function(){clickImage(id,btnsstat);}; //添加onclick事件
	}
}
//--====根据状态控制按钮图片====---------
function cti_qianru() {
//登录/签入状态
	showImage("btn1","../ctiHW/qianchu03.jpg",1,1);//按钮1：签入签出（当前状态为签入成功，显示按钮“签出”）
	showImage("btn2","../ctiHW/jieting02.jpg",0,0);//按钮2：接听
	showImage("btn3","../ctiHW/shixian03.jpg",1,1);//按钮3：示闲，示忙//enable???
	showImage("btn4","../ctiHW/xiaoxiu02.jpg",0,0);//按钮4：小休
	showImage("btn5","../ctiHW/guaduan02.jpg",0,0);//按钮5：挂断
	showImage("btn6","../ctiHW/sanfang02.jpg",0,0);//按钮6：三方，取消三方
	showImage("btn7","../ctiHW/zhuanjie02.jpg",0,0);//按钮7：转接，取消转接
	showImage("btn8","../ctiHW/hujiao03.jpg",1,0);//按钮8：btnsstat 0呼叫
	showImage("btn9","../ctiHW/baochi02.jpg",0,0);//按钮9：保持
	//btns[0]=0;//全局变量，初始状态
}
function cti_qianchu() {
//注销/签出状态
	// 除签入按钮外屏蔽所有功能
	showImage("btn1","../ctiHW/qianru03.jpg",1,0);//按钮1：签入签出（当前状态为签出成功，显示按钮“签入”）
	showImage("btn2","../ctiHW/jieting02.jpg",0,0);//按钮2：接听
	showImage("btn3","../ctiHW/shixian02.jpg",0,0);//按钮3：示闲，示忙
	showImage("btn4","../ctiHW/xiaoxiu02.jpg",0,0);//按钮4：小休
	showImage("btn5","../ctiHW/guaduan02.jpg",0,0);//按钮5：挂断
	showImage("btn6","../ctiHW/sanfang02.jpg",0,0);//按钮6：三方，取消三方
	showImage("btn7","../ctiHW/zhuanjie02.jpg",0,0);//按钮7：转接，取消转接
	showImage("btn8","../ctiHW/hujiao02.jpg",0,0);//按钮8：呼叫
	showImage("btn9","../ctiHW/baochi02.jpg",0,0);//按钮9：保持
}
function cti_shixian() {
//示闲
	showImage("btn3","../ctiHW/shixian03.jpg",1,1);//crt
}
function cti_shimang() {
//示忙
	showImage("btn3","../ctiHW/shimang03.jpg",1,0);//crt
}
function cti_baochi() {
//保持
	showImage("btn9","../ctiHW/baochi03.jpg",1,1);//按钮9：0取保持 1保持r
	showImage("btn7","../ctiHW/zhuanjie03.jpg",1,0);//按钮7：0转接

}
function cti_jieting() {
//有电话进来的时候 才能显示“接听”和“挂断”按钮
	showImage("btn1","../ctiHW/qianru01.jpg",0,0);//按钮1：签入签出（当前状态为签入成功，显示按钮“签出”）
	showImage("btn2","../ctiHW/1691486994940.gif",1,0);//按钮2：接听
	showImage("btn3","../ctiHW/shimang02.jpg",0,0);//按钮3：示闲，示忙
	showImage("btn4","../ctiHW/xiaoxiu02.jpg",0,0);//按钮4：小休
	showImage("btn5","../ctiHW/guaduan03.jpg",1,0);//按钮5：挂断
	showImage("btn6","../ctiHW/sanfang02.jpg",0,0);//按钮6：三方，取消三方
	showImage("btn7","../ctiHW/zhuanjie02.jpg",0,0);//按钮7：转接，取消转接
	showImage("btn8","../ctiHW/hujiao02.jpg",0,0);//按钮8：呼叫
	showImage("btn9","../ctiHW/baochi02.jpg",0,0);//按钮9：保持
}
//--====事件====---------
//登录、签入
function zdlogin(forceLogin)
{
	try {
		var agentId = 80030;
		var secret = 123456;
		var phone = 60001;

		cti.login(agentId, secret, phone, false, forceLogin,
			{
				success: function(result) {
					$.modal.msgSuccess('签入成功');
					cti_qianru();
					document.getElementById('ztai').innerHTML='示忙';
				},

				error: function(e) {

					if(e.code == 31) {
						if (confirm("该坐席已经登录，是否强制登录？")) {
							zdlogin(true);
						}
					}
					else {
						$.modal.msgError("登录失败. " + e.message);
					}
				}
			});
	}
	catch(e) {
		alert(e.message);
	}
}
//注销、签出
function logout()
{
	try {
		cti.logout(
			{
				success: function(result) {
					$.modal.msgSuccess('注销成功');
					cti_qianchu();
					document.getElementById('ztai').innerHTML='签出';
				},

				error: function(e) {
					$.modal.msgError("注销失败. " + e.message);
				}
			});
	}
	catch(e) {
		btnAgentLogout.disabled = false;
		alert(e.message);
	}
}
//示忙
function setNotReady()
{
	try {
		cti.setNotReady();
		//图片示闲
		cti_shixian();
		document.getElementById('ztai').innerHTML='示忙';

	}
	catch(e) {
		alert(e.message);
	}
}
//示闲
function setReady()
{
	try {
		cti.setReady();
		//图片示忙
		cti_shimang();
		document.getElementById('ztai').innerHTML='示闲';

	}
	catch(e) {
		alert(e.message);
	}
}
//转接弹框
function inputzj(){
	layer.open({
		type: 1,
		area: ['500px', '300px'],
		fix: false,
		maxmin: false,
		shade: 0.3,
		skin: 'demo-class',
		title: '转接',
		content: $('#againBack'),
		btn: ['确定', '关闭'],
		// 弹层外区域关闭
		shadeClose: false,
		//同意
		yes: function(index, layero) {
			var hciphone= $('#mobile').val();

			if(hciphone == null || hciphone.length == 0) {
				alert("请输入电话号码.");
				return;
			}
			transOut(hciphone);
			layer.close(index);
		},
		//关闭
		cancel: function(index) {
			return true;
		}
	});
}
//转接
function transOut(number)
{
	try {
		debugger
		cti.transOut(number);
		showImage("btn7","../ctiHW/zhuanjie01.jpg",1,1);//按钮7：0转接，1取消转接

	}
	catch(e) {
		alert(e.message);
	}
}
//呼叫弹框
function inputMobile(){
	layer.open({
		type: 1,
		area: ['500px', '300px'],
		fix: false,
		maxmin: false,
		shade: 0.3,
		skin: 'demo-class',
		title: '请输入手机号码',
		content: $('#againBack'),
		btn: ['确定', '关闭'],
		// 弹层外区域关闭
		shadeClose: false,
		//同意
		yes: function(index, layero) {
			var hciphone= $('#mobile').val();

			if(hciphone == null || hciphone.length == 0) {
				alert("请输入电话号码.");
				return;
			}
			callOut(hciphone);
				layer.close(index);
		},
		//关闭
		cancel: function(index) {
			return true;
		}
	});
}
//呼叫事件
function callOut(number)
{
	try {
		cti.callOut(null, number,
			{
				success: function(e) {
					showImage("btn8","../ctiHW/hujiao01.jpg",0,1);//按钮8：btnsstat 0呼叫
					showImage("btn5","../ctiHW/guaduan03.jpg",1,0);//按钮5：btnsstat 0挂断

					document.getElementById('ztai').innerHTML='呼叫中...';
				},
				error: function(e) {
					alert(e.message);
				}
			});
	}
	catch(e) {
		$.modal.msgError("呼叫失败. " + e.message);

	}
}
//挂断
function releaseCall()
{
	try {
		cti.releaseCall();
	}
	catch(e) {
		$.modal.msgError("挂断失败. " + e.message);
	}
}
//保持
function holdCall()
{
	try {
		cti.holdCall(
			{
				success: function(result) {
					showImage("btn9","../ctiHW/baochi01.jpg",1,0);//按钮9：保持
					console.log("hold call success. ");
				},
				error: function(result) {
					$.modal.msgError("保持失败. " + e.message);
				}
			});
	}
	catch(e) {
		alert(e.message);
	}
}
//取保持
function retrieveHold()
{
	try {
		cti.retrieveHold(heldCallId,
			{
				success: function(result) {
					showImage("btn9","../ctiHW/baochi03.jpg",1,1);//按钮9：取保持

					console.log("retrieve call success. ");
				},
				error: function(result) {
					$.modal.msgError("取保持失败. " + e.message);
				}
			});
	}
	catch(e) {
		alert(e.message);
	}
}
//接听
function answerCall()
{
	try {
		cti.answerCall();
		showImage("btn2","../ctiHW/jieting01.jpg",0,1);//按钮2：接听

	}
	catch(e) {
		alert(e.message);
	}
}
//--====控制图片状态====---------
//点击图片
function clickImage(id,btnsstat){
	switch(id) {
		case "btn1"://按钮1：签入签出
			if (btnsstat==0){
				zdlogin();//签入
			}
			else {
				logout();//注销
			}
			break;
		case "btn2"://按钮2：接听
			if (btnsstat==0) { 
				answerCall();//接听
			}
			break;
		case "btn3"://按钮3：示闲，示忙
			if (btnsstat==0){
				setNotReady();//示忙
			}
			else{
				setReady();//示闲
			}
			break;
		case "btn4"://按钮4：小休
			if (btnsstat==0){
				// SetNotReady();//示忙
			}
			else{
				;
			}
			break;
		case "btn5"://按钮5：挂断
			if (btnsstat==0){
				releaseCall();//挂断
			}
			else{
				;
			}
			break;
		case "btn6"://按钮6：三方，取消三方
			if (btnsstat==0){
				//InitialConference();//初始化3方通话
				//CompleteConference();//完成3方通话
				// window.showModalDialog("../ctiHW/ziConference.html",window,"dialogWidth:520px;dialogHeight:300px;help:no;scroll:no;status:no;");
			}
			else{
				// CancelConference();//取消3方通话
			}
			break;
		case "btn7"://按钮7：转接，取消转接
			if (btnsstat==0) {
				inputzj();
			}

			break;
		case "btn8"://按钮8：呼叫
			if (btnsstat==0){
				//CallAgent();//呼叫
				inputMobile();
			}
			else {
				ReleaseCall();//挂断
			}
			break;
		case "btn9"://按钮9：呼叫保持/取消保持
			if (btnsstat==0){
				retrieveHold();//按钮9：取消保持

			}else{
				holdCall();//按钮9：呼叫保持
			}
			break;

		default:
			break;
	}
}